package com.sense.kukoo.biz_explore.adapter.provider

import android.view.View
import android.widget.ImageView
import android.widget.TextView
import com.alibaba.android.arouter.utils.TextUtils
import com.sense.kukoo.biz_explore.adapter.ExploreContentAdapter
import com.sense.kukoo.common.R
import com.sense.kukoo.common.repo.bean.BookBean
import com.sense.kukoo.common.repo.bean.ExploreContentBean
import com.sense.kukoo.common.utils.ApplicationContext
import com.sense.kukoo.common.wiget.adapter.provider.MultiItemProvider
import com.sense.kukoo.common.wiget.adapter.viewholder.BaseViewHolder
import com.sense.kukoo.lib_logger.LoggerManager
import com.sense.kukoo.lib_picture.config.GlideConfig
import com.sense.kukoo.lib_picture.loader.Monet

class RecommendedItemProvider(val mListener: ExploreContentAdapter.ExploreContentEventListener) :
    MultiItemProvider<ExploreContentBean>() {

    private val TAG: String = javaClass.simpleName

    override fun getItemType(): Int = ExploreContentBean.TYPE_1_X_N

    override fun getItemLayoutId(): Int = R.layout.common_recommended_item

    override fun onBindViewHolder(viewHolder: BaseViewHolder, bean: ExploreContentBean) {
        val book: BookBean = bean.book!!
        //点击事件
        viewHolder.getView(R.id.card_view).setOnClickListener {
            mListener.onClickBook(bean, book)
        }
        val iv_music_play = viewHolder.getView(R.id.iv_music_play)
        iv_music_play.setOnClickListener {
            mListener.onClickBookPlay(bean, book)
        }
        //内容
        val tv_title = viewHolder.getView(R.id.tv_title) as TextView
        if (bean.isFirst) {
            tv_title.text = bean.title
            tv_title.visibility = View.VISIBLE
        } else {
            tv_title.visibility = View.GONE
        }

        //
        val iv_music_icon = viewHolder.getView(R.id.iv_music_icon) as ImageView
        Monet.get(ApplicationContext.instance())
            .load<GlideConfig>(book.coverUrl)
            .placeholder(R.mipmap.common_icon_default_editor_picks)
            .into(iv_music_icon)

        val tv_music_type = viewHolder.getView(R.id.tv_music_type) as TextView
        when (book.type) {
            BookBean.TYPE_UNKNOW -> {
                tv_music_type.setText(ApplicationContext.getString(com.sense.kukoo.common_i18n.R.string.explore_type_unknow))
            }
            BookBean.TYPE_AUDIOBOOK -> {
                tv_music_type.setText(ApplicationContext.getString(com.sense.kukoo.common_i18n.R.string.explore_type_audiobook))
            }
            BookBean.TYPE_PODCAST -> {
                tv_music_type.setText(ApplicationContext.getString(com.sense.kukoo.common_i18n.R.string.explore_type_podcast))
            }
            BookBean.TYPE_SHORT_AUDIO -> {
                tv_music_type.setText(ApplicationContext.getString(com.sense.kukoo.common_i18n.R.string.explore_type_short_audio))
            }
            else -> {}
        }
        val tv_music_des = viewHolder.getView(R.id.tv_music_collection) as TextView
        tv_music_des.text = if (TextUtils.isEmpty(book.alias)) book.name else book.alias

        val tv_music_msg = viewHolder.getView(R.id.tv_music_msg) as TextView
        val tv_listens = viewHolder.getView(R.id.tv_listens) as TextView
        //
        if(book.isChapter==1){
            tv_listens.text = "${book.listeners ?: 0}"
            tv_music_msg.visibility = View.INVISIBLE
            tv_listens.visibility = View.VISIBLE
        }else{
            tv_music_msg.text = "${book.subscribers ?: 0}"
            tv_music_msg.visibility = View.VISIBLE
            tv_listens.visibility = View.INVISIBLE
        }

        //todo 标签处理
        LoggerManager.d(TAG,"book.keywords=${book.keywords}")
        val tv_music_tag_1 = viewHolder.getView(R.id.tv_music_tag_1) as TextView
        val tv_music_tag_2 = viewHolder.getView(R.id.tv_music_tag_2) as TextView
        if(!TextUtils.isEmpty(book.keywords)){
            val split = book.keywords!!.split("，")
            if(split.size>1){
                tv_music_tag_1.text = split[0]
                tv_music_tag_1.visibility = View.VISIBLE
            }else{
                tv_music_tag_1.visibility = View.GONE
            }
            if(split.size>2){
                tv_music_tag_2.text = split[1]
                tv_music_tag_2.visibility = View.VISIBLE
            }else{
                tv_music_tag_2.visibility = View.GONE
            }
        }
    }
}